package com.cty.controller;

import com.cty.pojo.User;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("e")
@ResponseBody
public class EController {

    /*
    * @Validated：实体类上有校验注解加上此注解即可生效
    *   BindingResult 当校验的参数出现问题的时候，
    *   该对象可以捕捉到错误的信息，从而做出正确的处理方法
    *
    * */
    @PostMapping("register")
    public Object register(@Validated @RequestBody User user, BindingResult result) {
        if (result.hasErrors()) {
            Map data = new HashMap();
            data.put("code",404);
            data.put("message",result.getFieldError().toString());
            return data;
        }
        return user;
    }

    @GetMapping("data")//NullPointerException
    public String data() {
//        String data = null;
//        data.toString();
        return "ok";
    }

    @GetMapping("data1")//ArithmeticException
    public String data1() {
//        int a = 1/0;
        return "ok";
    }
}
